<?php

namespace Rebecca\RebeccaBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Rebecca\RebeccaBundle\Entity\Periodos;
use Rebecca\RebeccaBundle\Form\PeriodosType;

/**
 * Periodos controller.
 *
 */
class PeriodosController extends Controller
{
    /**
     * Lists all Periodos entities.
     *
     */
    public function indexAction()
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosComponent */
        $component = $this->get("components.Periodos");
        $entities = $component->getAll();

        return $this->render('RebeccaBundle:Periodos:index.html.twig', array(
            'entities' => $entities
        ));
    }

    /**
     * Finds and displays a Periodos entity.
     *
     */
    public function showAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosComponent */
        $component = $this->get("components.Periodos");

        $entity = $component->get($id);
        
        $conceptos = $component->getConceptos($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Periodos entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('RebeccaBundle:Periodos:show.html.twig', array(
            'entity'      => $entity,
            'conceptos'   => $conceptos,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to create a new Periodos entity.
     *
     */
    public function newAction()
    {
        $entity = new Periodos();
        $form   = $this->createForm(new PeriodosType(), $entity);

        return $this->render('RebeccaBundle:Periodos:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView()
        ));
    }
    
    /**
     * Displays a form to activate Periodos entity.
     *
     */
    public function activateAction()
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosComponent */
        $component = $this->get("components.Periodos");
        
        $periodos = $component->getAll();
        
        $request = $this->getRequest();
        
        $error = $request->get("error");
        
        if(isset ($error)){
            $error = true;
        }else{
            $error = false;
        }
        
        return $this->render('RebeccaBundle:Periodos:activarPeriodo.html.twig', array(
            'periodos'     => $periodos,
            'error'        => $error,
        ));
    }
    
    public function confirmAction()
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosComponent */
        $component = $this->get("components.Periodos");
        
        $request = $this->getRequest();
        $idPeriodo = $request->get("idPeriodo");
        
        if(isset ($idPeriodo)){
            $component->addPeriodosLaboralesContratos($idPeriodo);
            
            return $this->redirect($this->generateUrl('periodos'));
        }else{
            return $this->redirect($this->generateUrl('periodos_activate', array('error'=> '1')));
        }
    }

    /**
     * Creates a new Periodos entity.
     *
     */
    public function createAction()
    {
        $entity  = new Periodos();
        $request = $this->getRequest();
        $form    = $this->createForm(new PeriodosType(), $entity);
        $form->bindRequest($request);

        if ($form->isValid()) {
            /* @var $component Rebecca\RebeccaBundle\Component\PeriodosComponent */
            $component = $this->get("components.Periodos");
            $component->save($entity);
            $component->commit();

            return $this->redirect($this->generateUrl('periodos'));
            
        }

        return $this->render('RebeccaBundle:Periodos:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView()
        ));
    }

    /**
     * Displays a form to edit an existing Periodos entity.
     *
     */
    public function editAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosComponent */
        $component = $this->get("components.Periodos");
        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Periodos entity.');
        }

        $editForm = $this->createForm(new PeriodosType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('RebeccaBundle:Periodos:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Edits an existing Periodos entity.
     *
     */
    public function updateAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosComponent */
        $component = $this->get("components.Periodos");

        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Periodos entity.');
        }

        $editForm   = $this->createForm(new PeriodosType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();

        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            $component->update($entity);
            $component->commit();

            return $this->redirect($this->generateUrl('periodos'));
        }

        return $this->render('RebeccaBundle:Periodos:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a Periodos entity.
     *
     */
    public function deleteAction($id)
    {
        $form = $this->createDeleteForm($id);
        $request = $this->getRequest();

        $form->bindRequest($request);

        if ($form->isValid()) {
            /* @var $component Rebecca\RebeccaBundle\Component\PeriodosComponent */
            $component = $this->get("components.Periodos");
            $entity = $component->get($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Periodos entity.');
            }

            $component->delete($entity);
            $component->commit();
        }

        return $this->redirect($this->generateUrl('periodos'));
    }

    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
}
